using System;
using System.Configuration;
using System.Text;
using System.Windows;
using System.Net;
using System.Net.Sockets;
using System.Windows.Controls;
using System.Threading;
using System.Windows.Threading;
using System.Collections.Generic;
using System.IO;


namespace ServerApplication
{
    namespace PeerManager
    {
        ///-------------------------------------------------------------------------------------------------
        /// <summary>   A peer </summary>
        ///
        /// <remarks>   Adrian, 08/11/2015. </remarks>
        ///-------------------------------------------------------------------------------------------------

        class Peer
        {
            private IPAddress m_ipAddress;
            private int m_portNumber;
            private string mFileName;
            private bool mStatus;
            private string mPeerNick;

            public string PeerNick
            {
                get 
                {
                    return mPeerNick;
                }

                set 
                {
                    mPeerNick = value;
                }
            }


            public bool Status
            {
                get
                {
                    return mStatus;
                }

                set
                {
                    mStatus = value;
                }
            }


            public int PortNumber
            {
                get
                {
                    return m_portNumber;
                }

                set
                {
                    m_portNumber = value;
                }
            }

            public IPAddress IpAddress
            {
                get
                {
                    return m_ipAddress;
                }

                set
                {
                    m_ipAddress = value;
                }
            }

            public Peer(IPAddress p_ipAddress, int p_portNumber)
            {
                this.IpAddress = p_ipAddress;
                this.PortNumber = p_portNumber;
            }

            public string ToString()
            {
                return IpAddress.ToString() + ":" + PortNumber.ToString();
            }
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary>   Manager for peers. </summary>
        ///
        /// <remarks>   Adrian, 08/11/2015. </remarks>
        ///-------------------------------------------------------------------------------------------------
       
        class PeerManager
        {
            public PeerManager()
            {
                mAllPeers = new Dictionary<string, Peer>();
                mAllStatus = new Dictionary<string, CurrentStatus>();
            }

            public void AddPeer(Peer p_currentPeer)
            {
                mAllPeers.Add(p_currentPeer.ToString(), p_currentPeer);
            }

            public void AddPeer(string pIpPort, string pPeerName)
            {
                string[] portIp = pIpPort.Split(':');
                mAllPeers.Add(pIpPort, new Peer(IPAddress.Parse(portIp[0]), Convert.ToInt32(portIp[1])));
                mAllStatus.Add(pPeerName, new CurrentStatus(pIpPort));
            }

            public void RemovePeer(string p_peerString)
            {
                mAllPeers.Remove(p_peerString);
            }

            public Peer GetPeer(string p_inputString)
            {
                return mAllPeers[p_inputString];
            }

            public Dictionary<string, Peer> GetPeer()
            {
                return mAllPeers;
            }

            public List<Peer> GetPeerList()
            {
                return new List<Peer>(mAllPeers.Values);
            }

            public void AddTestStatus()
            {
                mAllStatus["test"] = new CurrentStatus("test");
            }

            public void ChangeStatus(string pPeerName)
            {
                Peer currentPeer = mAllPeers[mAllStatus[pPeerName].IpPortRepresentation];
                currentPeer.Status = !currentPeer.Status;
            }


            public Dictionary<string, Peer> mAllPeers;
            public Dictionary<string, CurrentStatus> mAllStatus;
        }

    }
}
